package com.example.goodpet.my.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.utils.Constants;

import java.util.List;

public class MyPetCollectAdapter extends  RecyclerView.Adapter<MyPetCollectAdapter.ViewHolder> {
    private List<UserPetInfo> lists;
    private Context context;

    public MyPetCollectAdapter(List<UserPetInfo> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_pet_collect, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        UserPetInfo userPetInfo = lists.get(position);
        Glide.with(context).load(Constants.BASE_URL_IMAGE+userPetInfo.getPetImage().get(0)).into(holder.ivPetCollect);
        Glide.with(context).load(Constants.BASE_URL_IMAGE+userPetInfo.getPortrait()).into(holder.portrait);
        holder.nickname.setText(userPetInfo.getNickname());
        holder.tvOverview.setText(userPetInfo.getOverview());

        /**
         * 设置点击跳转到宠物详情页面的监听
         */
        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PetInfoActivity.class);
                UserPetInfo userPetInfo1 = lists.get(position);
                intent.putExtra("userPetInfo",userPetInfo1);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPetCollect;
        TextView tvOverview;
        ImageView portrait;
        TextView nickname;
        LinearLayout layoutEnter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetCollect=itemView.findViewById(R.id.ivPetCollect);
            tvOverview=itemView.findViewById(R.id.tvOverview);
            portrait=itemView.findViewById(R.id.portrait);
            nickname=itemView.findViewById(R.id.nickname);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);
        }
    }
}
